<template>
  <view class="incoming-container">

    <!-- 筛选条件区域 -->
    <!-- 筛选条件区域 -->
    <filter-bar :filters="['department', 'contractStatus', 'filter']" :filter-data="filterData"
      @filter-change="handleFilterChange"></filter-bar>

    <!-- 合同列表 -->
    <scroll-view scroll-y class="contract-list" @scrolltolower="loadMore">
      <view class="contract-item" v-for="(item, index) in contractList" :key="index">
        <view class="status-tag">将搬入</view>
        <view class="contract-content">
          <view class="electric-tag">电</view>
          <view class="contract-info">
            <view class="contract-title">{{item.title}}</view>
            <view class="contract-detail">
              <text class="date">{{item.start_date}} 至 {{item.end_date}}</text>
              <text class="tenant">| {{item.tenant}}</text>
            </view>
            <view class="contract-status">
              <text class="days-left">{{item.days_left}}天后搬入</text>
              <text class="tag" v-if="item.tag">{{item.tag}}</text>
            </view>
          </view>
        </view>
      </view>

      <!-- 加载更多 -->
      <view class="loading" v-if="isLoading">
        <text>加载中...</text>
      </view>
      <view class="no-more" v-if="noMore">
        <text>没有更多数据了</text>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import NavBar from '@/components/nav-bar/index.vue';
import FilterBar from '@/components/filter-bar/index.vue';

export default {
  components: {
    NavBar,
    FilterBar
  },
  data() {
    return {
      contractList: [
        {
          title: '尚文创业城6号1单元403·3',
          department: '总部 | 尹吉鑫',
          start_date: '2025-02-23',
          end_date: '2025-06-22',
          tenant: '尹吉鑫',
          days_left: 2,
          tag: ''
        },
        {
          title: '尚文创业城6号1单元403·8',
          department: '总部 | 吴康',
          start_date: '2025-02-23',
          end_date: '2026-02-22',
          tenant: '吴康',
          days_left: 2,
          tag: ''
        },
        {
          title: '山水华庭5号3单元502·1',
          department: '总部 | 吴浩',
          start_date: '2025-02-24',
          end_date: '2025-08-23',
          tenant: '吴浩',
          days_left: 3,
          tag: ''
        },
        {
          title: '保利华都5号1单元1904·2',
          department: '总部 | 谢宽',
          start_date: '2025-02-24',
          end_date: '2026-02-23',
          tenant: '谢宽',
          days_left: 3,
          tag: ''
        },
        {
          title: '金地天悦5号1单元1101·3',
          department: '总部 | 邓婉妮',
          start_date: '2025-02-24',
          end_date: '2025-06-23',
          tenant: '邓婉妮',
          days_left: 3,
          tag: ''
        },
        {
          title: '金地天悦5号1单元401·3',
          department: '总部 | 廖子旋',
          start_date: '2025-02-25',
          end_date: '2026-02-24',
          tenant: '廖子旋',
          days_left: 4,
          tag: ''
        }
      ],
      isLoading: false,
      noMore: false,
      page: 1,
      pageSize: 10
    }
  },
  onLoad() {
    // 页面加载时获取合同数据
    this.getContractList();
  },
  methods: {
    goBack() {
      uni.navigateBack();
    },
    refreshList() {
      // 刷新列表数据
      this.page = 1;
      this.noMore = false;
      this.getContractList(true);
    },
    showSearch() {
      // 跳转到搜索页面
      uni.navigateTo({
        url: '/pages/contract/search'
      });
    },
    toggleFilter(type) {
      // 跳转到对应的筛选页面
      uni.navigateTo({
        url: `/pages/contract/filter?type=${type}`
      });
    },
    toggleSort() {
      // 跳转到排序页面
      uni.navigateTo({
        url: '/pages/contract/sort'
      });
    },
    async getContractList(refresh = false) {
      try {
        this.isLoading = true;
        
        if (refresh) {
          this.contractList = [];
          this.page = 1;
        }
        
        // TODO: 替换为实际的API调用
        // const response = await this.$api.getContractList({
        //   page: this.page,
        //   pageSize: this.pageSize
        // });
        
        // 模拟API响应数据
        const newData = [
          {
            title: '测试房源' + this.page,
            department: '总部 | 测试',
            start_date: '2025-02-23',
            end_date: '2025-06-22',
            tenant: '测试用户',
            days_left: this.page,
            tag: ''
          }
        ];
        
        this.contractList = [...this.contractList, ...newData];
        this.noMore = this.page >= 3;
        
        if (!this.noMore) {
          this.page++;
        }
      } catch (error) {
        // 错误处理
        uni.showToast({
          title: '加载失败，请重试',
          icon: 'none'
        });
        console.error('获取合同列表失败:', error);
      } finally {
        this.isLoading = false;
      }
    },
    loadMore() {
      if (!this.isLoading && !this.noMore) {
        this.getContractList();
      }
    }
  }
}
</script>

<style lang="scss">
.incoming-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.contract-list {
  padding: 20rpx;
  height: calc(100vh - 180rpx);
  box-sizing: border-box;
}

.contract-item {
  background-color: #fff;
  margin-bottom: 20rpx;
  border-radius: 12rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  padding: 24rpx;
  transition: transform 0.2s ease;
  position: relative;
  
  &:active {
    transform: scale(0.98);
  }
}

.status-tag {
  position: absolute;
  top: 24rpx;
  right: 0;
  padding: 2rpx 8rpx;
  background-color: #ff9800;
  color: #fff;
  font-size: 20rpx;
  border-radius: 8rpx 0 0 8rpx;
  line-height: 1.4;
}

.contract-content {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  position: relative;
  
  .electric-tag {
    position: absolute;
    top: 0;
    left: 0rpx;
    background-color: #41b5b3;
    color: #fff;
    padding: 0;
    font-size: 24rpx;
    border-radius: 6rpx;
    z-index: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 40rpx;
    height: 40rpx;
    margin-right: 10rpx;
  }
  
  .contract-info {
    flex: 1;
    min-width: 0;
  }

  .contract-title {
    font-size: 28rpx;
    font-weight: 600;
    color: #333;
    margin-bottom: 16rpx;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.4;
    margin-left: 50rpx;
  }

  .contract-detail {
    font-size: 28rpx;
    color: #666;
    margin-bottom: 16rpx;
    line-height: 1.5;
    display: flex;
    flex-wrap: wrap;
    gap: 16rpx;
    
    text {
      display: inline-flex;
      align-items: center;
      white-space: nowrap;
      
      &.department {
        color: #666;
        font-weight: 500;
        font-size: 22rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
      
      &.date, &.tenant {
        color: #666;
        font-size: 22rpx;
      }
    }
  }

  .contract-status {
    font-size: 24rpx;
    color: #999;
    line-height: 1.4;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 16rpx;
    
    text {
      display: inline-block;
      
      &.days-left {
        color: #ff6666;
        background-color: #f5f5f5;
        padding: 4rpx 12rpx;
        border-radius: 4rpx;
        font-size: 20rpx;
      }
    }
  }
}

.loading,
.no-more {
  text-align: center;
  padding: 20rpx 0;
  color: #999;
  font-size: 24rpx;
}
</style>